package cn.xorange.commons.core.utils;

import cn.xorange.commons.core.basic.bean.SystemCode;
import cn.xorange.commons.core.exception.SysException;
import cn.xorange.commons.utils.cache.ICacheService;
import cn.xorange.commons.core.constants.SysConstants;
import cn.xorange.commons.utils.spring.SpringUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

/**
 * @author : yangjian
 * date : 2024/7/16
 * description :
 */
public class SecretUtils {
    SecretUtils(){}

    public static String[] getIdAndSecret(String dictType){
        String appIdSecretJson = SpringUtils.getBean(ICacheService.class).getCacheObject(SysConstants.APPID_SECRET_SYS_DICT_NAME).toString();
        JSONArray array =  JSON.parseArray(appIdSecretJson);
        for(Object o : array ){
            JSONObject obj = (JSONObject)o;
            if (obj.getString(SysConstants.DICT_LABEL).equals(dictType)){
                return new String[]{obj.getString(SysConstants.DICT_VALUE), obj.getString(SysConstants.DICT_REMARK)};
            }
        }
        throw new SysException(SystemCode.SYSTEM_APPID_SECRET_EXCEPTION);
    }

    public static String getSecret(String appid){
        String appIdSecretJson = SpringUtils.getBean(ICacheService.class).getCacheObject(SysConstants.APPID_SECRET_SYS_DICT_NAME).toString();
        JSONArray array =  JSON.parseArray(appIdSecretJson);
        for(Object o : array ){
            JSONObject obj = (JSONObject)o;
            if (obj.getString(SysConstants.DICT_VALUE).equals(appid)){
                return obj.getString(SysConstants.DICT_REMARK);
            }
        }
        throw new SysException(SystemCode.SYSTEM_APPID_SECRET_EXCEPTION);
    }
}
